from product_upload.util.basic.mysql_util import db_get_one


class BasicProductMap:
    # 字段顺序与数据库字段一致
    fields = ['basic_id', 'status', 'sku', 'basic_price', 'basic_quantity','create_time', 'update_time','update_by','amazon_price', 'amazon_quantity','first_zero_time']

    def __init__(self, **kwargs):
        self.basic_id = kwargs.get('basic_id', None)
        self.status = kwargs.get('status', None)
        self.sku = kwargs.get('sku', None)
        self.basic_price = kwargs.get('basic_price', "")
        self.basic_quantity = kwargs.get('basic_quantity', None)
        self.create_time = kwargs.get('create_time', None)
        self.sku = kwargs.get('sku', None)
        self.update_time = kwargs.get('update_time', None)
        self.update_by = kwargs.get('update_by', None)
        self.amazon_price = kwargs.get('amazon_price', None)
        self.amazon_quantity = kwargs.get('amazon_quantity', None)
        self.first_zero_time = kwargs.get('first_zero_time', None)

    @classmethod
    def from_db(cls, field_names, row) -> 'BasicProduct':
        if len(field_names) != len(row):
            raise ValueError("bug!---db field len != query field len")
        data_dict = dict(zip(field_names, row))
        return cls(**data_dict)

    def __repr__(self):
        field_info = []
        for field in self.fields:
            value = getattr(self, field)
            field_info.append(f"{field}={repr(value)}")
        return f"BasicProductMap=[{', '.join(field_info)}]"


if __name__ == '__main__':
    print(db_get_one("basic_product_map", "basic_id=3", BasicProductMap))
    pass
